local liwang_active = fk.CreateSkill{
  name = "hanqing__liwang&",
}

Fk:loadTranslationTable{
  ["hanqing__liwang&"] = "利往",
  [":hanqing__liwang&"] = "出牌阶段，你可以声明一种你可以使用的伤害牌（无距离次数限制），郭开可以允许你视为对其指定的唯一目标使用之，"..
  "然后你需将手牌中一张装备牌置于郭开的装备区。若其场上牌数未因此增加，其失去〖利往〗。",

  ["#hanqing__liwang&"] = "利往：声明一种伤害牌，郭开可以允许你视为对其指定的角色使用",
  ["#hanqing__liwang-ask"] = "利往：你可以选择一名角色，视为 %dest 对其使用【%arg】",
  ["#hanqing__liwang-put"] = "利往：请将一张装备牌置入 %src 的装备区，否则其将失去“利往”！",
}

liwang_active:addEffect("active", {
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#hanqing__liwang&",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("bt")
    all_names = table.filter(all_names, function (name)
      return Fk:cloneCard(name).is_damage_card
    end)
    local names = table.filter(all_names, function (name)
      local card = Fk:cloneCard(name)
      card.skillName = "hanqing__liwang"
      return player:canUse(card, { bypass_distances = true, bypass_times = true }) and not player:prohibitUse(card)
    end)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  can_use = function (self, player)
    return player:getMark("hanqing__liwang_fail-phase") == 0 and
      table.find(Fk:currentRoom().alive_players, function (p)
        return p ~= player and p:hasSkill("hanqing__liwang")
      end)
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = table.find(room:getOtherPlayers(player, false), function (p)
      return p ~= player and p:hasSkill("hanqing__liwang")
    end)
    if not target then return end
    room:sendLog{
      type = "#Choice",
      from = player.id,
      arg = self.interaction.data,
      toast = true,
    }
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = "hanqing__liwang"
    local targets = table.filter(room.alive_players, function (p)
      return player:canUseTo(card, p, { bypass_distances = true, bypass_times = true }) and not player:isProhibited(p, card)
    end)
    if #targets == 0 then
      room:setPlayerMark(player, "hanqing__liwang_fail-phase", 1)
      return
    end
    local to = room:askToChoosePlayers(target, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = "hanqing__liwang",
      prompt = "#hanqing__liwang-ask::"..player.id..":"..self.interaction.data,
      cancelable = true,
    })
    if #to == 0 then
      room:setPlayerMark(player, "hanqing__liwang_fail-phase", 1)
      return
    end
    to = to[1]
    room:useVirtualCard(self.interaction.data, nil, player, to, "hanqing__liwang", true)
    if target.dead then return end
    if player.dead or player:isKongcheng() then
      room:handleAddLoseSkills(target, "-hanqing__liwang")
    else
      local cards = table.filter(player:getCardIds("h"), function (id)
        local c = Fk:getCardById(id)
        return c.type == Card.TypeEquip and target:canMoveCardIntoEquip(id, false)
      end)
      local id = room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        include_equip = false,
        skill_name = "hanqing__liwang",
        pattern = tostring(Exppattern{ id = cards }),
        prompt = "#hanqing__liwang-put:"..target.id,
        cancelable = true,
      })
      if #id == 0 then
        room:handleAddLoseSkills(target, "-hanqing__liwang")
      else
        room:moveCardIntoEquip(target, id, "hanqing__liwang", false, player)
      end
    end
  end,
})

return liwang_active
